﻿using System;
using System.Drawing;
using Size = System.Windows.Size;

namespace Com.Lancetinc.Evs.Helpers
{
    /// <summary>
    /// 纸张大小帮助类
    /// </summary>
    public static class PaperSizeHelper
    {
        /// <summary>
        /// 取得A4纸对应的尺寸(wpf尺寸，用于显示控件)
        /// </summary>
        /// <returns></returns>
        public static Size GetA4PaperSize()
        {
            var w = 210;
            var h = 297;
            return new Size(MillimeterToWpfUnits(w), MillimeterToWpfUnits(h));
        }

        public static float MillimeterToWpfUnits(float millimeter)
        {
            return InchToWpfUnits(MillimeterToInch(millimeter));
        }

        public static float WpfUnitsToMillimeter(float unit)
        {
            return InchToMillimeter(WpfUnitsToInch((unit)));
        }
        
        // inch 英寸=25.4 millimeter 毫米 
        public static float MillimeterToInch(float millimeter)
        {
            return millimeter / 25.4f;
        }

        public static float InchToMillimeter(float inch)
        {
            return inch * 25.4f;
        }

        private static float _dpi = -1;
        private static float DPI
        {
            get
            {
                if (_dpi > 0)
                    return _dpi;

                using Graphics g = Graphics.FromHwnd(IntPtr.Zero);
                _dpi = g.DpiX;
                return _dpi;
            }
        }

        public static float InchToWpfUnits(float inch)
        {
            return inch * DPI;
        }

        public static float WpfUnitsToInch(float units)
        {
            return units / DPI;
        }
    }
}
